<!DOCTYPE html>
<html class="client-nojs vector-feature-night-mode-disabled vector-feature-language-in-header-enabled vector-feature-language-in-main-page-header-disabled vector-feature-page-tools-pinned-disabled vector-feature-toc-pinned-clientpref-1 vector-feature-main-menu-pinned-disabled vector-feature-limited-width-clientpref-1 vector-feature-limited-width-content-enabled vector-feature-custom-font-size-clientpref-1 vector-feature-appearance-pinned-clientpref-1 vector-sticky-header-enabled" lang="en" dir="ltr"><head>
<meta charset="UTF-8">
<title>Trait (computer programming)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="canonical" href="https://en.wikipedia.org/wiki/Trait_(computer_programming)"> <link href="./mw/ext.cite.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/ext.pygments.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.icons.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.search.codex.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/skins.vector.styles.css" rel="stylesheet" type="text/css">
<link href="./mw/user.styles.css" rel="stylesheet" type="text/css">
<meta name="ResourceLoaderDynamicStyles" content="">
<link rel="stylesheet" type="text/css" href="./mw/site.styles.css">
<link rel="stylesheet" type="text/css" href="./mw/noscript.css">
<link rel="stylesheet" type="text/css" href="./footer.css">
<link rel="stylesheet" type="text/css" href="./vector-2022.css">
</head>
<body class="skin--responsive skin-vector skin-vector-search-vue mediawiki ltr sitedir-ltr mw-hide-empty-elt ns-0 ns-subject page-Trait_computer_programming rootpage-Trait_computer_programming skin-vector-2022 action-view">
<div class="mw-page-container">
<div class="mw-page-container-inner">
<div class="mw-content-container">
<main id="content" class="mw-body">
<header class="mw-body-header vector-page-titlebar">
<h1 id="firstHeading" class="firstHeading mw-first-heading">
<span id="openzim-page-title" class="mw-page-title-main"><span class="mw-page-title-main">Trait (computer programming)</span></span>
</h1>
</header>
<a id="top"></a>
<div id="bodyContent" class="vector-body ve-init-mw-desktopArticleTarget-targetContainer" aria-labelledby="firstHeading" data-mw-ve-target-container="">
<div id="mw-content-text" class="mw-body-content mw-content-ltr" lang="en" dir="ltr"><div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr">
<style data-mw-deduplicate="TemplateStyles:r1251242444">
/* start https://en.wikipedia.org/ */
.mw-parser-output .ambox{border:1px solid #a2a9b1;border-left:10px solid #36c;background-color:#fbfbfb;box-sizing:border-box}.mw-parser-output .ambox+link+.ambox,.mw-parser-output .ambox+link+style+.ambox,.mw-parser-output .ambox+link+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+style+.ambox,.mw-parser-output .ambox+.mw-empty-elt+link+link+.ambox{margin-top:-1px}html body.mediawiki .mw-parser-output .ambox.mbox-small-left{margin:4px 1em 4px 0;overflow:hidden;width:238px;border-collapse:collapse;font-size:88%;line-height:1.25em}.mw-parser-output .ambox-speedy{border-left:10px solid #b32424;background-color:#fee7e6}.mw-parser-output .ambox-delete{border-left:10px solid #b32424}.mw-parser-output .ambox-content{border-left:10px solid #f28500}.mw-parser-output .ambox-style{border-left:10px solid #fc3}.mw-parser-output .ambox-move{border-left:10px solid #9932cc}.mw-parser-output .ambox-protection{border-left:10px solid #a2a9b1}.mw-parser-output .ambox .mbox-text{border:none;padding:0.25em 0.5em;width:100%}.mw-parser-output .ambox .mbox-image{border:none;padding:2px 0 2px 0.5em;text-align:center}.mw-parser-output .ambox .mbox-imageright{border:none;padding:2px 0.5em 2px 0;text-align:center}.mw-parser-output .ambox .mbox-empty-cell{border:none;padding:0;width:1px}.mw-parser-output .ambox .mbox-image-div{width:52px}@media(min-width:720px){.mw-parser-output .ambox{margin:0 10%}}@media print{body.ns-0 .mw-parser-output .ambox{display:none!important}}
/* end https://en.wikipedia.org/ */
</style>
<p>In <a href="Computer_programming" title="Computer programming">computer programming</a>, a <b>trait</b> is a <a href="Programming_language" title="Programming language">language</a> concept that represents a set of <a href="Method_(computer_programming)" title="Method (computer programming)">methods</a> that can be used to extend the functionality of a <a href="Class_(computer_science)" class="mw-redirect" title="Class (computer science)">class</a>.<sup id="cite_ref-schaerli-ecoop-2003_1-0" class="reference"><a href="#cite_note-schaerli-ecoop-2003-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span class="cite-bracket">[</span>2<span class="cite-bracket">]</span></a></sup>
</p>
<meta property="mw:PageProp/toc">
<div class="mw-heading mw-heading2"><h2 id="Rationale">Rationale</h2></div>
<p>In object-oriented programming, behavior is sometimes shared between classes which are not related to each other. For example, many unrelated classes may have methods to <a href="Serialization" title="Serialization">serialize</a> objects to <a href="JSON" title="JSON">JSON</a>. Historically, there have been several approaches to solve this without duplicating the code in every class needing the behavior. Other approaches include <a href="Multiple_inheritance" title="Multiple inheritance">multiple inheritance</a> and <a href="Mixin" title="Mixin">mixins</a>, but these have drawbacks: the behavior of the code may unexpectedly change if the order in which the mixins are applied is altered, or if new methods are added to the parent classes or mixins.
</p><p>Traits solve these problems by allowing classes to use the trait and get the desired behavior. If a class uses more than one trait, the order in which the traits are used does not matter. The methods provided by the traits have direct access to the data of the class.
</p>
<div class="mw-heading mw-heading2"><h2 id="Characteristics">Characteristics</h2></div>
<p>Traits combine aspects of <a href="Protocol_(object-oriented_programming)" class="mw-redirect" title="Protocol (object-oriented programming)">protocols (interfaces)</a> and <a href="Mixins" class="mw-redirect" title="Mixins">mixins</a>. Like an interface, a trait defines one or more <a href="Function_prototype" title="Function prototype">method signatures</a>, of which implementing classes must provide implementations. Like a mixin, a trait provides additional behavior for the implementing class.
</p><p>In case of a <a href="Naming_collision" title="Naming collision">naming collision</a> between methods provided by different traits, the programmer must explicitly disambiguate which one of those methods will be used in the class; thus manually solving the <i><a href="Multiple_inheritance#The_diamond_problem" title="Multiple inheritance">diamond problem</a></i> of <a href="Multiple_inheritance" title="Multiple inheritance">multiple inheritance</a>. This is different from other composition methods in object-oriented programming, where conflicting names are automatically resolved by <a href="Scope_(computer_science)" title="Scope (computer science)">scoping rules</a>.
</p><p>Operations which can be performed with traits include:<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span class="cite-bracket">[</span>3<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span class="cite-bracket">[</span>4<span class="cite-bracket">]</span></a></sup>
</p>
<ul><li><i>symmetric sum</i>: an operation that merges two disjoint traits to create a new trait</li>
<li><i>override</i> (or <i>asymmetric sum</i>): an operation that forms a new trait by adding methods to an existing trait, possibly <a href="Method_overriding" title="Method overriding">overriding</a> some of its methods</li>
<li><i>alias</i>: an operation that creates a new trait by adding a new name for an existing method</li>
<li><i>exclusion</i>: an operation that forms a new trait by removing a method from an existing trait. (Combining this with the alias operation yields a <i>shallow rename</i> operation).</li></ul>
<p>If a method is excluded from a trait, that method must be provided by the class that consumes the trait, or by a parent class of that class. This is because the methods provided by the trait might call the excluded method.
</p><p>Trait composition is <a href="Commutative" class="mw-redirect" title="Commutative">commutative</a> (i.e. given traits <i>A</i> and <i>B</i>, <i>A</i> + <i>B</i> is equivalent to <i>B</i> + <i>A</i>) and <a href="Associative" class="mw-redirect" title="Associative">associative</a> (i.e. given traits <i>A</i>, <i>B</i>, and <i>C</i>, (<i>A</i> + <i>B</i>) + <i>C</i> is equivalent to <i>A</i> + (<i>B</i> + <i>C</i>)).<sup id="cite_ref-schaerli-ecoop-2003_1-1" class="reference"><a href="#cite_note-schaerli-ecoop-2003-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup>
</p>
<div class="mw-heading mw-heading2"><h2 id="Limitations">Limitations</h2></div>
<p>While traits offer significant advantages over many alternatives, they do have their own limitations.
</p>
<div class="mw-heading mw-heading3"><h3 id="Required_methods">Required methods</h3></div>
<p>If a trait requires the consuming class to provide certain methods, the trait cannot know if those methods are <a href="Semantic_equivalence" class="mw-redirect" title="Semantic equivalence">semantically equivalent</a> to the trait's needs. For some dynamic languages, such as Perl, the required method can only be identified by a method name, not a full <a href="Type_signature" title="Type signature">method signature</a>, making it harder to guarantee that the required method is appropriate.
</p>
<div class="mw-heading mw-heading3"><h3 id="Excluding_methods">Excluding methods</h3></div>
<p>If a method is excluded from a trait, that method becomes a 'required' method for the trait because the trait's other methods might call it.
</p>
<div class="mw-heading mw-heading2"><h2 id="Supported_languages">Supported languages</h2></div>
<p>Traits come originally from the programming language <a href="Self_(programming_language)" title="Self (programming language)">Self</a><sup id="cite_ref-5" class="reference"><a href="#cite_note-5"><span class="cite-bracket">[</span>5<span class="cite-bracket">]</span></a></sup> and are supported by the following programming languages:
</p>
<ul><li><a href="AmbientTalk" title="AmbientTalk">AmbientTalk</a>: Combines the properties of Self traits (object-based multiple inheritance) and <a href="Smalltalk" title="Smalltalk">Smalltalk</a>'s <a href="Squeak" title="Squeak">Squeak</a> traits (requiring explicit composition of traits by the programmer). It builds on the research on <i>stateful</i> and <i>freezable</i> traits to enable state within traits, which was not allowed in the first definitions.<sup id="cite_ref-6" class="reference"><a href="#cite_note-6"><span class="cite-bracket">[</span>6<span class="cite-bracket">]</span></a></sup></li>
<li><a href="C_Sharp_(programming_language)" title="C Sharp (programming language)">C#</a>: Since version 8.0, C# has support for <i>default interface methods</i>,<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span class="cite-bracket">[</span>7<span class="cite-bracket">]</span></a></sup> which have some properties of traits.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span class="cite-bracket">[</span>8<span class="cite-bracket">]</span></a></sup></li>
<li><a href="C%2B%2B" title="C++">C++</a>: Used in <a href="Standard_Template_Library" title="Standard Template Library">Standard Template Library</a> and the <a href="C%2B%2B_Standard_Library" title="C++ Standard Library">C++ Standard Library</a> to support generic <a href="Container_(abstract_data_type)" title="Container (abstract data type)">container classes</a><sup id="cite_ref-9" class="reference"><a href="#cite_note-9"><span class="cite-bracket">[</span>9<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-10" class="reference"><a href="#cite_note-10"><span class="cite-bracket">[</span>10<span class="cite-bracket">]</span></a></sup> and in the <a href="Boost_C%2B%2B_Libraries" class="mw-redirect" title="Boost C++ Libraries">Boost</a> TypeTraits library.<sup id="cite_ref-11" class="reference"><a href="#cite_note-11"><span class="cite-bracket">[</span>11<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Curl_(programming_language)" title="Curl (programming language)">Curl</a>: Abstract classes as mixins permit method implementations and thus constitute traits by another name.</li>
<li><a href="Fortress_(programming_language)" title="Fortress (programming language)">Fortress</a><sup id="cite_ref-12" class="reference"><a href="#cite_note-12"><span class="cite-bracket">[</span>12<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Groovy_(programming_language)" class="mw-redirect" title="Groovy (programming language)">Groovy</a>: Since version 2.3<sup id="cite_ref-13" class="reference"><a href="#cite_note-13"><span class="cite-bracket">[</span>13<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Haskell_(programming_language)" class="mw-redirect" title="Haskell (programming language)">Haskell</a>: In Haskell, Traits are known as <a href="Type_class" title="Type class">Type classes</a>.</li>
<li><a href="Haxe" title="Haxe">Haxe</a>: Since version 2.4.0.<sup id="cite_ref-14" class="reference"><a href="#cite_note-14"><span class="cite-bracket">[</span>14<span class="cite-bracket">]</span></a></sup> Called <i>Static Extension</i><sup id="cite_ref-15" class="reference"><a href="#cite_note-15"><span class="cite-bracket">[</span>15<span class="cite-bracket">]</span></a></sup> in the manual, it uses <code>using</code> keyword</li>
<li><a href="Java_(programming_language)" title="Java (programming language)">Java</a>: Since version 8, Java has support for <i>default methods</i>,<sup id="cite_ref-16" class="reference"><a href="#cite_note-16"><span class="cite-bracket">[</span>16<span class="cite-bracket">]</span></a></sup> which have some properties of traits.<sup id="cite_ref-17" class="reference"><a href="#cite_note-17"><span class="cite-bracket">[</span>17<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-18" class="reference"><a href="#cite_note-18"><span class="cite-bracket">[</span>18<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-19" class="reference"><a href="#cite_note-19"><span class="cite-bracket">[</span>19<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-20" class="reference"><a href="#cite_note-20"><span class="cite-bracket">[</span>20<span class="cite-bracket">]</span></a></sup></li>
<li><a href="JavaScript" title="JavaScript">JavaScript</a>: Traits can be implemented via functions and delegations<sup id="cite_ref-21" class="reference"><a href="#cite_note-21"><span class="cite-bracket">[</span>21<span class="cite-bracket">]</span></a></sup> or through libraries that provide traits.<sup id="cite_ref-22" class="reference"><a href="#cite_note-22"><span class="cite-bracket">[</span>22<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-23" class="reference"><a href="#cite_note-23"><span class="cite-bracket">[</span>23<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-24" class="reference"><a href="#cite_note-24"><span class="cite-bracket">[</span>24<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Julia_(programming_language)" title="Julia (programming language)">Julia</a>: Several packages implement traits, e.g.,<sup id="cite_ref-25" class="reference"><a href="#cite_note-25"><span class="cite-bracket">[</span>25<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Kotlin_(programming_language)" title="Kotlin (programming language)">Kotlin</a>: Traits have been called <i>interfaces</i><sup id="cite_ref-26" class="reference"><a href="#cite_note-26"><span class="cite-bracket">[</span>26<span class="cite-bracket">]</span></a></sup> since M12.<sup id="cite_ref-27" class="reference"><a href="#cite_note-27"><span class="cite-bracket">[</span>27<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Lasso_(programming_language)" title="Lasso (programming language)">Lasso</a><sup id="cite_ref-28" class="reference"><a href="#cite_note-28"><span class="cite-bracket">[</span>28<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Mojo_(programming_language)" title="Mojo (programming language)">Mojo</a>: Since version 0.6.0<sup id="cite_ref-29" class="reference"><a href="#cite_note-29"><span class="cite-bracket">[</span>29<span class="cite-bracket">]</span></a></sup></li>
<li><a href="OCaml" title="OCaml">OCaml</a>: Traits can be implemented using a variety of language features: module and module type inclusion, functors and functor types, class and class type inheritance, et cetera.</li>
<li><a href="Perl" title="Perl">Perl</a>: Called <i>roles</i>, they are implemented in Perl libraries such as <a href="Moose_(Perl)" title="Moose (Perl)">Moose</a>, Role::Tiny and Role::Basic. Roles are part of the sister language <a href="Raku_(programming_language)" title="Raku (programming language)">Raku</a>. <sup id="cite_ref-30" class="reference"><a href="#cite_note-30"><span class="cite-bracket">[</span>30<span class="cite-bracket">]</span></a></sup> With the acceptance of the <i>Corinna OOP Proposal</i><sup id="cite_ref-31" class="reference"><a href="#cite_note-31"><span class="cite-bracket">[</span>31<span class="cite-bracket">]</span></a></sup> Perl will have roles native to the language as part of a modern OOP system.</li>
<li><a href="PHP" title="PHP">PHP</a>: Since version 5.4,<sup id="cite_ref-32" class="reference"><a href="#cite_note-32"><span class="cite-bracket">[</span>32<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-php:rfc:horizontalreuse_33-0" class="reference"><a href="#cite_note-php:rfc:horizontalreuse-33"><span class="cite-bracket">[</span>33<span class="cite-bracket">]</span></a></sup> PHP allows users to specify templates that provide the ability to "inherit" from more than one (trait-)class, as a pseudo <a href="Multiple_inheritance" title="Multiple inheritance">multiple inheritance</a>.</li>
<li><a href="Python_(programming_language)" title="Python (programming language)">Python</a>: Via a third-party library,<sup id="cite_ref-34" class="reference"><a href="#cite_note-34"><span class="cite-bracket">[</span>34<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-35" class="reference"><a href="#cite_note-35"><span class="cite-bracket">[</span>35<span class="cite-bracket">]</span></a></sup> or via higher-order mixin classes<sup id="cite_ref-36" class="reference"><a href="#cite_note-36"><span class="cite-bracket">[</span>36<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Racket_(programming_language)" title="Racket (programming language)">Racket</a>: Supports traits as a library and uses macros, structures, and first-class classes to implement them.<sup id="cite_ref-37" class="reference"><a href="#cite_note-37"><span class="cite-bracket">[</span>37<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Ruby_(programming_language)" title="Ruby (programming language)">Ruby</a>: <i>Module mixins</i> can be used to implement traits.<sup id="cite_ref-38" class="reference"><a href="#cite_note-38"><span class="cite-bracket">[</span>38<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Rust_(programming_language)" title="Rust (programming language)">Rust</a><sup id="cite_ref-39" class="reference"><a href="#cite_note-39"><span class="cite-bracket">[</span>39<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Scala_(programming_language)" title="Scala (programming language)">Scala</a><sup id="cite_ref-40" class="reference"><a href="#cite_note-40"><span class="cite-bracket">[</span>40<span class="cite-bracket">]</span></a></sup><sup id="cite_ref-41" class="reference"><a href="#cite_note-41"><span class="cite-bracket">[</span>41<span class="cite-bracket">]</span></a></sup> trait is builtin supported with the key word <code>trait</code>.</li>
<li><a href="Smalltalk" title="Smalltalk">Smalltalk</a>: Traits are implemented in two dialects of Smalltalk, <a href="Squeak" title="Squeak">Squeak</a><sup id="cite_ref-schaerli-ecoop-2003_1-2" class="reference"><a href="#cite_note-schaerli-ecoop-2003-1"><span class="cite-bracket">[</span>1<span class="cite-bracket">]</span></a></sup> and <a href="Pharo" title="Pharo">Pharo</a>.<sup id="cite_ref-42" class="reference"><a href="#cite_note-42"><span class="cite-bracket">[</span>42<span class="cite-bracket">]</span></a></sup></li>
<li><a href="Swift_(programming_language)" title="Swift (programming language)">Swift</a>: Traits can be implemented with <i>protocol extensions</i>.<sup id="cite_ref-43" class="reference"><a href="#cite_note-43"><span class="cite-bracket">[</span>43<span class="cite-bracket">]</span></a></sup></li></ul>
<div class="mw-heading mw-heading2"><h2 id="Examples">Examples</h2></div>
<div class="mw-heading mw-heading3"><h3 id="C#">C#</h3></div><p>
On C# 8.0, it is possible to define an implementation as a member of an interface.</p><div class="mw-highlight mw-highlight-lang-c# mw-content-ltr" dir="ltr"><pre><span class="k">using</span><span class="w"> </span><span class="nn">System</span><span class="p">;</span>
<span class="k">namespace</span><span class="w"> </span><span class="nn">CSharp8NewFeatures</span><span class="p">;</span>
<span class="k">interface</span><span class="w"> </span><span class="n">ILogger</span>
<span class="p">{</span>
<span class="w"> </span><span class="c1">// Traditional interface methods</span>
<span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">Log</span><span class="p">(</span><span class="kt">string</span><span class="w"> </span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">LogError</span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">exception</span><span class="p">);</span>
<span class="w"> </span><span class="c1">// Default interface method</span>
<span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">LogWarning</span><span class="p">(</span><span class="kt">string</span><span class="w"> </span><span class="n">message</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span><span class="w"> </span>
<span class="p">}</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Logger</span><span class="w"> </span><span class="p">:</span><span class="w"> </span><span class="n">ILogger</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">Log</span><span class="p">(</span><span class="kt">string</span><span class="w"> </span><span class="n">message</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">message</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="k">public</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">LogError</span><span class="p">(</span><span class="n">Exception</span><span class="w"> </span><span class="n">exception</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">Console</span><span class="p">.</span><span class="n">WriteLine</span><span class="p">(</span><span class="n">exception</span><span class="p">.</span><span class="n">ToString</span><span class="p">());</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span><span class="w"> </span><span class="nc">Program</span>
<span class="p">{</span>
<span class="w"> </span><span class="k">static</span><span class="w"> </span><span class="k">void</span><span class="w"> </span><span class="nf">Main</span><span class="p">(</span><span class="kt">string</span><span class="p">[]</span><span class="w"> </span><span class="n">args</span><span class="p">)</span>
<span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">ILogger</span><span class="w"> </span><span class="n">logger</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">new</span><span class="w"> </span><span class="n">Logger</span><span class="p">();</span>
<span class="w"> </span><span class="n">logger</span><span class="p">.</span><span class="n">LogWarning</span><span class="p">(</span><span class="s">"Some warning message"</span><span class="p">);</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="PHP">PHP</h3></div>
<p>This example uses a trait to enhance other classes:
</p>
<div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span class="c1">// The template</span>
<span class="k">trait</span> <span class="nx">TSingleton</span>
<span class="p">{</span>
<span class="k">private</span> <span class="k">static</span> <span class="nv">$_instance</span> <span class="o">=</span> <span class="k">null</span><span class="p">;</span>
<span class="k">private</span> <span class="k">function</span> <span class="fm">__construct</span><span class="p">()</span> <span class="p">{}</span> <span class="c1">// Must have private default constructor and be aware not to open it in the class</span>
<span class="k">public</span> <span class="k">static</span> <span class="k">function</span> <span class="nf">getInstance</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="k">null</span> <span class="o">===</span> <span class="nx">self</span><span class="o">::</span><span class="nv">$_instance</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">self</span><span class="o">::</span><span class="nv">$_instance</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">self</span><span class="p">();</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">self</span><span class="o">::</span><span class="nv">$_instance</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">FrontController</span>
<span class="p">{</span>
<span class="k">use</span> <span class="nx">TSingleton</span><span class="p">;</span>
<span class="p">}</span>
<span class="c1">// Can also be used in already extended classes</span>
<span class="k">class</span> <span class="nc">WebSite</span> <span class="k">extends</span> <span class="nx">SomeClass</span>
<span class="p">{</span>
<span class="k">use</span> <span class="nx">TSingleton</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
<p>This allows simulating aspects of multiple inheritance:
</p>
<div class="mw-highlight mw-highlight-lang-php mw-content-ltr" dir="ltr"><pre><span class="k">trait</span> <span class="nx">TBounding</span>
<span class="p">{</span>
<span class="k">public</span> <span class="nv">$x</span><span class="p">,</span> <span class="nv">$y</span><span class="p">,</span> <span class="nv">$width</span><span class="p">,</span> <span class="nv">$height</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">trait</span> <span class="nx">TMoveable</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">moveTo</span><span class="p">(</span><span class="nv">$x</span><span class="p">,</span> <span class="nv">$y</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// …</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">trait</span> <span class="nx">TResizeable</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">resize</span><span class="p">(</span><span class="nv">$newWidth</span><span class="p">,</span> <span class="nv">$newHeight</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// …</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="k">class</span> <span class="nc">Rectangle</span>
<span class="p">{</span>
<span class="k">use</span> <span class="nx">TBounding</span><span class="p">,</span> <span class="nx">TMoveable</span><span class="p">,</span> <span class="nx">TResizeable</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">fillColor</span><span class="p">(</span><span class="nv">$color</span><span class="p">)</span>
<span class="p">{</span>
<span class="c1">// …</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
<div class="mw-heading mw-heading3"><h3 id="Rust">Rust</h3></div>
<p>A trait in Rust declares a set of methods that a type must implement.<sup id="cite_ref-44" class="reference"><a href="#cite_note-44"><span class="cite-bracket">[</span>44<span class="cite-bracket">]</span></a></sup> Rust compilers require traits to be explicated, which ensures the safety of <a href="Generic_programming" title="Generic programming">generics</a> in Rust.
</p>
<div class="mw-highlight mw-highlight-lang-rust mw-content-ltr" dir="ltr"><pre><span class="c1">// type T must have the "Ord" trait</span>
<span class="c1">// so that ">" and "<" operations can be done</span>
<span class="k">fn</span><span class="w"> </span><span class="nf">max</span><span class="o"><</span><span class="n">T</span><span class="p">:</span><span class="w"> </span><span class="nb">Ord</span><span class="o">></span><span class="p">(</span><span class="n">a</span><span class="p">:</span><span class="w"> </span><span class="kp">&</span><span class="p">[</span><span class="n">T</span><span class="p">])</span><span class="w"> </span><span class="p">-></span><span class="w"> </span><span class="nb">Option</span><span class="o"><&</span><span class="n">T</span><span class="o">></span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">a</span><span class="p">.</span><span class="n">first</span><span class="p">()</span><span class="o">?</span><span class="p">;</span>
<span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="n">n</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="n">a</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="o">*</span><span class="n">n</span><span class="w"> </span><span class="o">></span><span class="w"> </span><span class="o">*</span><span class="n">result</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="n">result</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&</span><span class="n">n</span><span class="p">;</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="p">}</span>
<span class="w"> </span><span class="nb">Some</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
<p>To simplify tedious and repeated implementation of traits like <code>Debug</code> and <code>Ord</code>, the <code>derive</code> macro can be used to request compilers to generate certain implementations automatically.<sup id="cite_ref-45" class="reference"><a href="#cite_note-45"><span class="cite-bracket">[</span>45<span class="cite-bracket">]</span></a></sup> Derivable traits include: <code>Clone</code>, <code>Copy</code>, <code>Debug</code>, <code>Default</code>, <code>PartialEq</code>, <code>Eq</code>, <code>PartialOrd</code>, <code>Ord</code> and <code>Hash</code>.
</p>
<div class="mw-heading mw-heading2"><h2 id="See_also">See also</h2></div>
<ul><li><a href="Extension_method" title="Extension method">Extension method</a></li>
<li><a href="Interface_(object-oriented_programming)" title="Interface (object-oriented programming)">Interface (object-oriented programming)</a></li>
<li><a href="Parametric_polymorphism" title="Parametric polymorphism">Parametric polymorphism</a></li>
<li><a href="UFCS" class="mw-redirect" title="UFCS">UFCS</a></li></ul>
<div class="mw-heading mw-heading2"><h2 id="References">References</h2></div>
<style data-mw-deduplicate="TemplateStyles:r1239543626">
/* start https://en.wikipedia.org/ */
.mw-parser-output .reflist{margin-bottom:0.5em;list-style-type:decimal}@media screen{.mw-parser-output .reflist{font-size:90%}}.mw-parser-output .reflist .references{font-size:100%;margin-bottom:0;list-style-type:inherit}.mw-parser-output .reflist-columns-2{column-width:30em}.mw-parser-output .reflist-columns-3{column-width:25em}.mw-parser-output .reflist-columns{margin-top:0.3em}.mw-parser-output .reflist-columns ol{margin-top:0}.mw-parser-output .reflist-columns li{page-break-inside:avoid;break-inside:avoid-column}.mw-parser-output .reflist-upper-alpha{list-style-type:upper-alpha}.mw-parser-output .reflist-upper-roman{list-style-type:upper-roman}.mw-parser-output .reflist-lower-alpha{list-style-type:lower-alpha}.mw-parser-output .reflist-lower-greek{list-style-type:lower-greek}.mw-parser-output .reflist-lower-roman{list-style-type:lower-roman}
/* end https://en.wikipedia.org/ */
</style><div class="reflist reflist-columns references-column-width" style="column-width: 30em;">
<ol class="references">
<li id="cite_note-schaerli-ecoop-2003-1"><span class="mw-cite-backlink">^ <a href="#cite_ref-schaerli-ecoop-2003_1-0"><sup><i><b>a</b></i></sup></a> <a href="#cite_ref-schaerli-ecoop-2003_1-1"><sup><i><b>b</b></i></sup></a> <a href="#cite_ref-schaerli-ecoop-2003_1-2"><sup><i><b>c</b></i></sup></a></span> <span class="reference-text"><style data-mw-deduplicate="TemplateStyles:r1238218222">
/* start https://en.wikipedia.org/ */
.mw-parser-output cite.citation{font-style:inherit;word-wrap:break-word}.mw-parser-output .citation q{quotes:"\"""\"""'""'"}.mw-parser-output .citation:target{background-color:rgba(0,127,255,0.133)}.mw-parser-output .id-lock-free.id-lock-free a{background:url("./mw/Lock-green.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-limited.id-lock-limited a,.mw-parser-output .id-lock-registration.id-lock-registration a{background:url("./mw/Lock-gray-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .id-lock-subscription.id-lock-subscription a{background:url("./mw/Lock-red-alt-2.svg")right 0.1em center/9px no-repeat}.mw-parser-output .cs1-ws-icon a{background:url("./mw/Wikisource-logo.svg")right 0.1em center/12px no-repeat}body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-free a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-limited a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-registration a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .id-lock-subscription a,body:not(.skin-timeless):not(.skin-minerva) .mw-parser-output .cs1-ws-icon a{background-size:contain;padding:0 1em 0 0}.mw-parser-output .cs1-code{color:inherit;background:inherit;border:none;padding:inherit}.mw-parser-output .cs1-hidden-error{display:none;color:var(--color-error,#d33)}.mw-parser-output .cs1-visible-error{color:var(--color-error,#d33)}.mw-parser-output .cs1-maint{display:none;color:#085;margin-left:0.3em}.mw-parser-output .cs1-kern-left{padding-left:0.2em}.mw-parser-output .cs1-kern-right{padding-right:0.2em}.mw-parser-output .citation .mw-selflink{font-weight:inherit}@media screen{.mw-parser-output .cs1-format{font-size:95%}html.skin-theme-clientpref-night .mw-parser-output .cs1-maint{color:#18911f}}@media screen and (prefers-color-scheme:dark){html.skin-theme-clientpref-os .mw-parser-output .cs1-maint{color:#18911f}}
/* end https://en.wikipedia.org/ */
</style><cite id="CITEREFSchärliDucasseNierstraszBlack2003" class="citation book cs1">Schärli, Nathanael; Ducasse, Stéphane; <a href="Oscar_Nierstrasz" title="Oscar Nierstrasz">Nierstrasz, Oscar</a>; Black, Andrew P. (2003). <a rel="nofollow" class="external text" href="http://scg.unibe.ch/archive/papers/Scha03aTraits.pdf">"Traits: Composable Units of Behaviour"</a> <span class="cs1-format">(PDF)</span>. <i>Proceedings of the European Conference on Object-Oriented Programming (ECOOP)</i>. Lecture Notes in Computer Science. Vol. 2743. Springer. pp. <span class="nowrap">248–</span>274. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1011.8">10.1.1.1011.8</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1007%2F978-3-540-45070-2_12">10.1007/978-3-540-45070-2_12</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-3-540-45070-2</bdi>.</cite></span>
</li>
<li id="cite_note-2"><span class="mw-cite-backlink"><b><a href="#cite_ref-2">^</a></b></span> <span class="reference-text"><cite id="CITEREFDucasseNierstraszSchärliWuyts2006" class="citation journal cs1">Ducasse, Stéphane; Nierstrasz, Oscar; Schärli, Nathanael; Wuyts, Roel; Black, Andrew P. (March 2006). "Traits: A mechanism for fine-grained reuse". <i>ACM Transactions on Programming Languages and Systems</i>. <b>28</b> (2): <span class="nowrap">331–</span>388. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.64.2480">10.1.1.64.2480</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F1119479.1119483">10.1145/1119479.1119483</a>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:16434119">16434119</a>.</cite></span>
</li>
<li id="cite_note-3"><span class="mw-cite-backlink"><b><a href="#cite_ref-3">^</a></b></span> <span class="reference-text"><cite id="CITEREFFisherReppy2003" class="citation report cs1"><a href="Kathleen_Fisher" title="Kathleen Fisher">Fisher, Kathleen</a>; Reppy, John (2003). <a rel="nofollow" class="external text" href="http://newtraell.cs.uchicago.edu/files/tr_authentic/TR-2003-13.pdf">Statically typed traits</a> <span class="cs1-format">(PDF)</span> (Report). <a href="University_of_Chicago" title="University of Chicago">University of Chicago</a>. <a rel="nofollow" class="external text" href="https://web.archive.org/web/20040517161301/http://www.cs.uchicago.edu/files/tr_authentic/TR-2003-13.pdf">Archived</a> <span class="cs1-format">(PDF)</span> from the original on May 17, 2004.</cite></span>
</li>
<li id="cite_note-4"><span class="mw-cite-backlink"><b><a href="#cite_ref-4">^</a></b></span> <span class="reference-text"><cite id="CITEREFFisherReppy2004" class="citation conference cs1">Fisher, Kathleen; Reppy, John (2004). <a rel="nofollow" class="external text" href="http://people.cs.uchicago.edu/~jhr/papers/2004/fool-traits.pdf"><i>A typed calculus of traits</i></a> <span class="cs1-format">(PDF)</span>. <a rel="nofollow" class="external text" href="http://www.cis.upenn.edu/~bcpierce/FOOL//FOOL11.html">11th Workshop on Foundations of Object-oriented Programming</a>. <a href="University_of_Chicago" title="University of Chicago">University of Chicago</a>.</cite></span>
</li>
<li id="cite_note-5"><span class="mw-cite-backlink"><b><a href="#cite_ref-5">^</a></b></span> <span class="reference-text"><cite id="CITEREFCurryBaerLipkieLee1982" class="citation conference cs1">Curry, Gael; Baer, Larry; Lipkie, Daniel; Lee, Bruce (1982). <i>Traits: An approach to multiple-inheritance subclassing</i>. SIGOA Conference on Office Information Systems. Philadelphia, Pennsylvania, USA: ACM Press. pp. <span class="nowrap">1–</span>9. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F966873.806468">10.1145/966873.806468</a>.</cite></span>
</li>
<li id="cite_note-6"><span class="mw-cite-backlink"><b><a href="#cite_ref-6">^</a></b></span> <span class="reference-text"><cite id="CITEREFVan_CutsemBergelDucasseDe_Meuter2009" class="citation conference cs1">Van Cutsem, Tom; Bergel, Alexandre; Ducasse, Stéphane; De Meuter, Wolfgang (2009). <a rel="nofollow" class="external text" href="http://soft.vub.ac.be/Publications/2009/vub-prog-tr-09-04.pdf"><i>Adding State and Visibility Control to Traits Using Lexical Nesting</i></a> <span class="cs1-format">(PDF)</span>. European Conference on Object-Oriented Programming (ECOOP 2009). Lecture Notes in Computer Science. Vol. 5653. Springer-Verlag. pp. <span class="nowrap">220–</span>243. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.372.1265">10.1.1.372.1265</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1007%2F978-3-642-03013-0_11">10.1007/978-3-642-03013-0_11</a>. <a href="ISBN_(identifier)" class="mw-redirect" title="ISBN (identifier)">ISBN</a> <bdi>978-3-642-03012-3</bdi>.</cite></span>
</li>
<li id="cite_note-7"><span class="mw-cite-backlink"><b><a href="#cite_ref-7">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.microsoft.com/en-gb/dotnet/csharp/whats-new/csharp-8#default-interface-methods">"Default interface methods"</a>. <i>What's new in C# 8.0</i>. Microsoft<span class="reference-accessdate">. Retrieved <span class="nowrap">November 29,</span> 2019</span>.</cite></span>
</li>
<li id="cite_note-8"><span class="mw-cite-backlink"><b><a href="#cite_ref-8">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://www.talkingdotnet.com/default-implementations-in-interfaces-in-c-sharp-8/">"Interfaces in C# 8.0 gets a makeover"</a>. <i>Default Implementation in Interfaces in C# 8.0</i>. Talking Dotnet. 9 September 2019<span class="reference-accessdate">. Retrieved <span class="nowrap">November 29,</span> 2019</span>.</cite></span>
</li>
<li id="cite_note-9"><span class="mw-cite-backlink"><b><a href="#cite_ref-9">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.sgi.com/tech/stl/iterator_traits.html">"iterator_traits<Iterator>"</a>. <i>Standard Template Library</i>. SGI.</cite></span>
</li>
<li id="cite_note-10"><span class="mw-cite-backlink"><b><a href="#cite_ref-10">^</a></b></span> <span class="reference-text"><cite id="CITEREFMyers1995" class="citation magazine cs1">Myers, Nathan C. (June 1995). <a rel="nofollow" class="external text" href="http://www.cantrip.org/traits.html">"Traits: a new and useful template technique"</a>. <i>C++ Report</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-11"><span class="mw-cite-backlink"><b><a href="#cite_ref-11">^</a></b></span> <span class="reference-text"><cite id="CITEREFAbrahams" class="citation web cs1">Abrahams, David. <a rel="nofollow" class="external text" href="http://www.boost.org/more/generic_programming.html#traits">"Generic Programming Techniques: Traits"</a>. <i>Boost C++ Libraries</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-12"><span class="mw-cite-backlink"><b><a href="#cite_ref-12">^</a></b></span> <span class="reference-text"><cite id="CITEREFSteeleMaessen2006" class="citation web cs1">Steele, Guy; Maessen, Jan-Willem (June 11, 2006). <a rel="nofollow" class="external text" href="http://stephane.ducasse.free.fr/Teaching/CoursAnnecy/0506-Master/ForPresentations/Fortress-PLDITutorialSlides9Jun2006.pdf">"Fortress Programming Language Tutorial"</a> <span class="cs1-format">(PDF)</span>. Sun Microsystems<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-13"><span class="mw-cite-backlink"><b><a href="#cite_ref-13">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.groovy-lang.org/objectorientation.html#_traits">"Object Orientation: Traits"</a>. <i>The Groovy Programming Language</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-14"><span class="mw-cite-backlink"><b><a href="#cite_ref-14">^</a></b></span> <span class="reference-text"><cite class="citation news cs1"><a rel="nofollow" class="external text" href="https://haxe.org/download/version/2.4.0/">"Haxe 2.4.0 - Haxe - The Cross-platform Toolkit"</a>. <i>Haxe - The Cross-platform Toolkit</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2017-09-12</span></span>.</cite></span>
</li>
<li id="cite_note-15"><span class="mw-cite-backlink"><b><a href="#cite_ref-15">^</a></b></span> <span class="reference-text"><cite class="citation news cs1"><a rel="nofollow" class="external text" href="https://haxe.org/manual/lf-static-extension.html">"Manual - Haxe - The Cross-platform Toolkit"</a>. <i>Haxe - The Cross-platform Toolkit</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2017-09-12</span></span>.</cite></span>
</li>
<li id="cite_note-16"><span class="mw-cite-backlink"><b><a href="#cite_ref-16">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html">"Default Methods"</a>. <i>The Java Tutorials</i>. Oracle<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-17"><span class="mw-cite-backlink"><b><a href="#cite_ref-17">^</a></b></span> <span class="reference-text"><cite id="CITEREFLiquoriSpiwack2008" class="citation journal cs1">Liquori, Luigi; Spiwack, Arnaud (2008). <a rel="nofollow" class="external text" href="https://hal.inria.fr/inria-00432538/">"FeatherTrait: A Modest Extension of Featherweight Java"</a>. <i>ACM Transactions on Programming Languages and Systems</i>. <b>30</b> (2): 11:1. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F1330017.1330022">10.1145/1330017.1330022</a></span>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:17231803">17231803</a>.</cite></span>
</li>
<li id="cite_note-18"><span class="mw-cite-backlink"><b><a href="#cite_ref-18">^</a></b></span> <span class="reference-text"><cite id="CITEREFLiquoriSpiwack2008" class="citation journal cs1">Liquori, Luigi; Spiwack, Arnaud (2008). <a rel="nofollow" class="external text" href="https://hal.inria.fr/inria-00432540/">"Extending FeatherTrait Java with Interfaces"</a>. <i>Theoretical Computer Science</i>. <b>398</b> (<span class="nowrap">1–</span>3): <span class="nowrap">243–</span>260. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://doi.org/10.1016%2Fj.tcs.2008.01.051">10.1016/j.tcs.2008.01.051</a></span>. <a href="S2CID_(identifier)" class="mw-redirect" title="S2CID (identifier)">S2CID</a> <a rel="nofollow" class="external text" href="https://api.semanticscholar.org/CorpusID:12923128">12923128</a>.</cite></span>
</li>
<li id="cite_note-19"><span class="mw-cite-backlink"><b><a href="#cite_ref-19">^</a></b></span> <span class="reference-text"><cite id="CITEREFBonoMensaNaddeo2014" class="citation conference cs1">Bono, Viviana; Mensa, Enrico; Naddeo, Marco (September 2014). <a rel="nofollow" class="external text" href="https://hal.inria.fr/hal-01026531/en"><i>Trait-oriented Programming in Java 8</i></a>. <a rel="nofollow" class="external text" href="http://pppj2014.pk.edu.pl/">International Conference on Principles and Practices of Programming on the Java Platform: virtual machines, languages, and tools (PPPJ ’14)</a>. pp. <span class="nowrap">181–</span>6. <a href="CiteSeerX_(identifier)" class="mw-redirect" title="CiteSeerX (identifier)">CiteSeerX</a> <span class="id-lock-free" title="Freely accessible"><a rel="nofollow" class="external text" href="https://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.902.161">10.1.1.902.161</a></span>. <a href="Doi_(identifier)" class="mw-redirect" title="Doi (identifier)">doi</a>:<a rel="nofollow" class="external text" href="https://doi.org/10.1145%2F2647508.2647520">10.1145/2647508.2647520</a>.</cite></span>
</li>
<li id="cite_note-20"><span class="mw-cite-backlink"><b><a href="#cite_ref-20">^</a></b></span> <span class="reference-text"><cite id="CITEREFForslund2016" class="citation web cs1">Forslund, Emil (February 3, 2016). <a rel="nofollow" class="external text" href="https://web.archive.org/web/20160804005218/http://ageofjava.com/2016/02/definition-of-trait-pattern-in-java.html">"Definition of the Trait Pattern in Java"</a>. <i>Age of Java</i>. Archived from <a rel="nofollow" class="external text" href="http://ageofjava.com/2016/02/definition-of-trait-pattern-in-java.html">the original</a> on August 4, 2016<span class="reference-accessdate">. Retrieved <span class="nowrap">February 3,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-21"><span class="mw-cite-backlink"><b><a href="#cite_ref-21">^</a></b></span> <span class="reference-text"><cite id="CITEREFSeliger2014" class="citation web cs1">Seliger, Peter (April 11, 2014). <a rel="nofollow" class="external text" href="http://peterseliger.blogspot.com/2014/04/the-many-talents-of-javascript.html">"The Many Talents of JavaScript"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2015</span>.</cite></span>
</li>
<li id="cite_note-22"><span class="mw-cite-backlink"><b><a href="#cite_ref-22">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://traitsjs.github.io/traits.js-website/">"Traits.js: Traits for JavaScript"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-23"><span class="mw-cite-backlink"><b><a href="#cite_ref-23">^</a></b></span> <span class="reference-text"><cite id="CITEREFVan_CutsemMiller2012" class="citation journal cs1">Van Cutsem, Tom; Miller, Mark S. (2012). <a rel="nofollow" class="external text" href="http://soft.vub.ac.be/Publications/2012/vub-soft-tr-12-19.pdf">"Robust Trait Composition for Javascript"</a> <span class="cs1-format">(PDF)</span>. <i>Science of Computer Programming</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-24"><span class="mw-cite-backlink"><b><a href="#cite_ref-24">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://cocktailjs.github.io/">"CocktailJS"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-25"><span class="mw-cite-backlink"><b><a href="#cite_ref-25">^</a></b></span> <span class="reference-text"><cite id="CITEREFMauro_Werder" class="citation web cs1">Mauro Werder. <a rel="nofollow" class="external text" href="https://github.com/mauro3/SimpleTraits.jl">"SimpleTraits.jl"</a>. <i><a href="GitHub" title="GitHub">GitHub</a></i><span class="reference-accessdate">. Retrieved <span class="nowrap">March 23,</span> 2017</span>.</cite></span>
</li>
<li id="cite_note-26"><span class="mw-cite-backlink"><b><a href="#cite_ref-26">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://kotlinlang.org/docs/reference/interfaces.html">"Interfaces"</a>. <i>Kotlin Reference</i>. JetBrains<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-27"><span class="mw-cite-backlink"><b><a href="#cite_ref-27">^</a></b></span> <span class="reference-text"><cite id="CITEREFBreslav2015" class="citation web cs1">Breslav, Andrey (May 29, 2015). <a rel="nofollow" class="external text" href="http://blog.jetbrains.com/kotlin/2015/05/kotlin-m12-is-out/">"Kotlin M12 is out!"</a>. <i>Kotlin Blog</i>. JetBrains<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-28"><span class="mw-cite-backlink"><b><a href="#cite_ref-28">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://lassoguide.com/language/traits.html">"Traits"</a>. <i>Lasso Language Guide</i>. LassoSoft. January 6, 2014<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-29"><span class="mw-cite-backlink"><b><a href="#cite_ref-29">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://docs.modular.com/mojo/changelog.html#v0.6.0-2023-12-04">"Modular Docs - Mojo🔥 changelog"</a>. <i>docs.modular.com</i><span class="reference-accessdate">. Retrieved <span class="nowrap">2023-12-13</span></span>.</cite></span>
</li>
<li id="cite_note-30"><span class="mw-cite-backlink"><b><a href="#cite_ref-30">^</a></b></span> <span class="reference-text"><cite id="CITEREFchromatic2009" class="citation web cs1">chromatic (April 30, 2009). <a rel="nofollow" class="external text" href="http://www.modernperlbooks.com/mt/2009/04/the-why-of-perl-roles.html">"The Why of Perl Roles"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-31"><span class="mw-cite-backlink"><b><a href="#cite_ref-31">^</a></b></span> <span class="reference-text"><cite id="CITEREFCurtis_"Ovid"_Poe" class="citation web cs1">Curtis "Ovid" Poe. <a rel="nofollow" class="external text" href="https://github.com/Ovid/Cor/blob/master/rfc/mvp.md">"Corinna OOP Proposal"</a>. <i>Corinna RFC</i><span class="reference-accessdate">. Retrieved <span class="nowrap">September 30,</span> 2022</span>.</cite></span>
</li>
<li id="cite_note-32"><span class="mw-cite-backlink"><b><a href="#cite_ref-32">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.php.net/manual/en/language.oop5.traits.php">"Traits"</a>. <i>PHP Documentation</i>. The PHP Group<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-php:rfc:horizontalreuse-33"><span class="mw-cite-backlink"><b><a href="#cite_ref-php:rfc:horizontalreuse_33-0">^</a></b></span> <span class="reference-text"><cite id="CITEREFMarr2011" class="citation web cs1">Marr, Stefan (January 9, 2011). <a rel="nofollow" class="external text" href="https://wiki.php.net/rfc/horizontalreuse">"Request for Comments: Horizontal Reuse for PHP"</a>. <i>PHP.net wiki</i>. The PHP Group<span class="reference-accessdate">. Retrieved <span class="nowrap">January 31,</span> 2011</span>.</cite></span>
</li>
<li id="cite_note-34"><span class="mw-cite-backlink"><b><a href="#cite_ref-34">^</a></b></span> <span class="reference-text"><cite id="CITEREFPerä" class="citation web cs1">Perä, Teppo. <a rel="nofollow" class="external text" href="http://py3traits.readthedocs.org/">"py3traits Documentation"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-35"><span class="mw-cite-backlink"><b><a href="#cite_ref-35">^</a></b></span> <span class="reference-text"><cite id="CITEREFPerä2015" class="citation web cs1">Perä, Teppo (2015-03-25). <a rel="nofollow" class="external text" href="https://github.com/Debith/py2traits">"py2traits"</a>. <i><a href="GitHub" title="GitHub">GitHub</a></i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-36"><span class="mw-cite-backlink"><b><a href="#cite_ref-36">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20161009062141/http://stupid-python-tricks.blogspot.com/2015/04/computed-properties-and-higher-order.html">"Higher Order Mixin Classes"</a>. Archived from <a rel="nofollow" class="external text" href="http://stupid-python-tricks.blogspot.com/2015/04/computed-properties-and-higher-order.html">the original</a> on 2016-10-09.</cite></span>
</li>
<li id="cite_note-37"><span class="mw-cite-backlink"><b><a href="#cite_ref-37">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://docs.racket-lang.org/reference/trait.html">"Traits"</a>. <i>The Racket Reference</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-38"><span class="mw-cite-backlink"><b><a href="#cite_ref-38">^</a></b></span> <span class="reference-text"><cite id="CITEREFDavid_Naseby2004" class="citation web cs1">David Naseby (February 14, 2004). <a rel="nofollow" class="external text" href="http://ruby-naseby.blogspot.com/2008/11/traits-in-ruby.html">"Traits in Ruby"</a>. <i>Ruby Naseby</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-39"><span class="mw-cite-backlink"><b><a href="#cite_ref-39">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://doc.rust-lang.org/book/ch10-02-traits.html">"Traits"</a>. <i>The Rust Programming Language</i><span class="reference-accessdate">. Retrieved <span class="nowrap">September 30,</span> 2019</span>.</cite></span>
</li>
<li id="cite_note-40"><span class="mw-cite-backlink"><b><a href="#cite_ref-40">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://www.scala-lang.org/node/126">"Traits"</a>. <i>A Tour of Scala</i>. <a href="%C3%89cole_polytechnique_f%C3%A9d%C3%A9rale_de_Lausanne" class="mw-redirect" title="École polytechnique fédérale de Lausanne">École polytechnique fédérale de Lausanne</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-41"><span class="mw-cite-backlink"><b><a href="#cite_ref-41">^</a></b></span> <span class="reference-text"><cite id="CITEREFNeward2008" class="citation web cs1">Neward, Ted (April 29, 2008). <a rel="nofollow" class="external text" href="http://www.ibm.com/developerworks/java/library/j-scala04298/">"The busy Java developer's guide to Scala: Of traits and behaviors"</a>. <i>IBM developerWorks</i>. IBM<span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-42"><span class="mw-cite-backlink"><b><a href="#cite_ref-42">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://pharo.gemtalksystems.com/book/LanguageAndLibraries/Traits/">"Traits in 10 minutes"</a>. <i>Pharo: The CollaborActive Book</i><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-43"><span class="mw-cite-backlink"><b><a href="#cite_ref-43">^</a></b></span> <span class="reference-text"><cite id="CITEREFHollemans2015" class="citation web cs1">Hollemans, Matthijs (July 22, 2015). <a rel="nofollow" class="external text" href="https://machinethink.net/blog/mixins-and-traits-in-swift-2.0/">"Mixins and Traits in Swift 2.0"</a><span class="reference-accessdate">. Retrieved <span class="nowrap">January 23,</span> 2016</span>.</cite></span>
</li>
<li id="cite_note-44"><span class="mw-cite-backlink"><b><a href="#cite_ref-44">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://web.archive.org/web/20230529022632/http://gradebot.org/doc/ipur/trait.html">"Traits - Introduction to Programming Using Rust"</a>. Archived from <a rel="nofollow" class="external text" href="http://gradebot.org/doc/ipur/trait.html">the original</a> on 2023-05-29.</cite></span>
</li>
<li id="cite_note-45"><span class="mw-cite-backlink"><b><a href="#cite_ref-45">^</a></b></span> <span class="reference-text"><cite class="citation web cs1"><a rel="nofollow" class="external text" href="https://doc.rust-lang.org/book/first-edition/traits.html">"Traits - the Rust Programming Language"</a>.</cite></span>
</li>
</ol></div>
<div class="mw-heading mw-heading2"><h2 id="External_links">External links</h2></div>
<ul><li><cite class="citation web cs1"><a rel="nofollow" class="external text" href="http://scg.unibe.ch/research/traits">"Traits: Composable Units of Behavior"</a>. <i>Software Composition Group</i>. University of Bern.</cite></li></ul></div><!--htdig_noindex--><div><div class="zim-footer">
This article is issued from <a class="external text" title="Last edited on 2025-08-04" href="https://en.wikipedia.org/wiki/?title=Trait_(computer_programming)&oldid=1304234841">Wikipedia</a>. The text is available under <a class="external text" href="https://creativecommons.org/licenses/by-sa/4.0/deed.en">Creative Commons Attribution-Share Alike 4.0</a> unless otherwise noted. Additional terms may apply for the media files.
</div>
</div><!--/htdig_noindex--></div>
</div>
</main>
</div>
</div>
</div>
</body></html>